package cn.darkhorse.mesadmin.controller.api;

import cn.darkhorse.common.util.HttpClientUtils;
import cn.darkhorse.device.pojo.MesDevice;
import cn.darkhorse.device.service.MesDeviceService;
import cn.darkhorse.pick.pojo.CottonApiModel;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.config.ApiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.sign.Md5Utils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/other/ecottone")
@ApiOperation(value = "第三方接口管理")
public class ApiController extends BaseController {
    @Autowired
    private ApiConfig apiConfig;
    @Autowired
    private MesDeviceService mesDeviceService;
    @RequestMapping("/pickMapList")
    @ApiOperation(value = "坐标信息")
    public AjaxResult pickMapList(String deviceCode,String startTime, String endTime) {
        String URL = "https://zs.ecottone.com/api/getPickMapList";


        JSONObject object = new JSONObject();

        object.put("deviceMainCode",deviceCode);
        object.put("startTime",startTime);
        object.put("endTime",endTime);
        String s = sendPost(URL, object);
        System.out.println(s);
        JSONObject parse = JSONObject.parse(s);

        return AjaxResult.success(parse);
    }

    @RequestMapping("/allPickMapList")
    @ApiOperation(value = "坐标信息")
    public AjaxResult allPickMapList(String startTime, String endTime) {
        List<MesDevice> deviceList = mesDeviceService.list();

        String URL = "https://zs.ecottone.com/api/getPickMapList";
        JSONArray array = new JSONArray();
        for (MesDevice device : deviceList) {
            String code = device.getCode();
            JSONObject object = new JSONObject();

            object.put("deviceMainCode",code);
            object.put("startTime",startTime);
            object.put("endTime",endTime);
            String s = sendPost(URL, object);


            JSONObject parse = JSONObject.parse(s);
            if (parse.getJSONArray("data")!=null){
                parse.getJSONArray("data").forEach(o->{
                    JSONObject jsonObject = (JSONObject) o;
                    jsonObject.put("deviceMainCode",code);
                    jsonObject.put("deviceName",device.getName());
                });
                array.addAll(parse.getJSONArray("data"));
            }
        }

        return AjaxResult.success(array);
    }

    @RequestMapping("/pickerList")
    @ApiOperation("采棉机")
    public AjaxResult pickerList() {
        String URL = "https://zs.ecottone.com/api/getPickerList";

        JSONObject object = new JSONObject();

        object.put("deptId",apiConfig.getDeptId());

        String s = sendPost(URL, object);

        JSONObject parse = JSONObject.parse(s);

        return AjaxResult.success(parse.getJSONArray("data"));
    }


    @RequestMapping("/pickInfoList")
    @ApiOperation("棉包信息")
    public AjaxResult pickInfoList(String deviceCode,String startTime, String endTime) {
        String URL = "https://zs.ecottone.com/api/getPickInfoList";

        JSONObject object = new JSONObject();
        object.put("deviceMainCode",deviceCode);
        object.put("startTime",startTime);
        object.put("endTime",endTime);

        String s = sendPost(URL, object);
        JSONObject parse = JSONObject.parse(s);
        return AjaxResult.success(parse.getJSONArray("data"));
    }



    private String sendPost(String URL, JSONObject object) {
        String timedStamp = timeStamp();
        String sign = Md5Utils.hash(apiConfig.getServiceName() + apiConfig.getKey() + timedStamp);
        CottonApiModel cottonApiModel = new CottonApiModel(apiConfig.getServiceName(), timedStamp, sign, com.alibaba.fastjson2.JSONObject.toJSONString(object));
        String jsonString = JSONObject.toJSONString(cottonApiModel);
        System.out.println(jsonString);
        String s = null;
        try {
            s = HttpClientUtils.doPostJson(URL, JSONObject.toJSONString(cottonApiModel));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        return s;
    }


    /**
     * 取得当前时间戳（精确到秒）
     * @return
     */
    public static String timeStamp(){
        long time = System.currentTimeMillis();
        String t = String.valueOf(time/1000);
        return t;
    }
}
